home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11107 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: gaia.ns.utk.edu!mbk
  2. From: mbk@caffeine.engr.utk.edu (Matt Kennel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating operator= for *pointers* to objects??
  5. Date: 12 Mar 1996 18:45:12 GMT
  6. Organization: University of Tennessee, Knoxville and Oak Ridge National Laboratory
  7. Message-ID: <4i4gno$psa@gaia.ns.utk.edu>
  8. References: <Do4KtH.MK@mv.mv.com> <4i4aqd$e2b@fsgi01.fnal.gov>
  9. Reply-To: kennel@msr.epm.ornl.gov
  10. NNTP-Posting-Host: caffeine.engr.utk.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. David Sachs (b91926@fsgi01.fnal.gov) wrote:
  14. : bnb@curtech.com (Brian Ballard) writes:
  15.  
  16.  
  17.  
  18. : >I'm trying to find a way to create an assignment (or copy) operator
  19. : >which I can use to apply to pointers to object.  For example, say I have
  20. : >a class hierarchy that looks like:
  21.  
  22. : >         |--- B
  23. : >    A ---|--- C
  24. : >         |--- D
  25.  
  26. : >Millions of these objects can potentially be created and scrapped every
  27. : >few minutes so, in order to improve performance and reduce memory
  28. : >fragmentation, every class in the hierarchy has their new/delete operators
  29. : >overloaded to use a handful of "pre-built" objects in the freestore.
  30. : >Pretty standard stuff.
  31.  
  32. : >Because exact duplicates of an instantiated object can (and must) exist,
  33. : >class A has a refCount member which allows me to keep track of how many
  34. : >copies of a duplicated object exist out there, so I don't pull the rug
  35. : >out from under an object that is still being used.
  36.  
  37. : >In general, megabytes worth of network packets are stored in a file, which
  38. : >get read in by this software and are wrapped up in one of the above derived
  39. : >classes (B, C, or D) depending upon the type of packet.  This creates a
  40. : >virtual "list" of sorts where I have an iterator which walks through the
  41. : >file, returning objects of type B/C/D.
  42.  
  43. : >Basically, I'd like to be able to do this:
  44.  
  45. : >{
  46. : > A*    start = NULL;
  47. : > A*    temp = NULL;
  48.  
  49. : > start = mStream->GetPacketAtOffset(0L);    // This might return an object B*
  50. : > temp = start;                              // I need this to call my oper=
  51. : > while ( temp = mStream->GetNextPacket(temp)) // GetNextPacket automatically
  52. : >    bla bla bla                               // 'delete's first A* temp
  53.  
  54. : >...
  55. : >} 
  56.  
  57. : >Not a big deal to it this way, but it's easy to forget over the long haul
  58. : >that these steps need to be taken.  It's much easier to do a simple pointer
  59. : >assignment which guarantees that every pointer to an object is accounted for.
  60.  
  61.  
  62. : >OK.... a bit much content for such an easy question....
  63.  
  64.  
  65. : Since operators cannot be overloaded for build-in types, you may
  66. : wish to define a "smart pointer" class to refer to your objects.
  67.  
  68. To me, this sounds like an excellent application for an automatic
  69. garbage collector.  A garbage collector will automatically free those 
  70. objects which are not being pointed to by any other live object.  You
  71. use conventional pointers as you always would. 
  72.  
  73. There is a free one on ftp://parcftp.xerox.com/pub/gc, I believe,
  74. and I have seen Usenet advertisements for commercial products for
  75. Unix and Windows. 
  76.  
  77. cheers
  78. Matt
  79.  
  80.  
  81. : -- 
  82. : ***** Listen Americans, the IRS is your taxer,  the IRS is one. *****
  83. : David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
  84. : Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
  85.